<?php

/***

LZRoster: A roster for World of Warcraft.
Copyright (C) 2007  Emmanuel Cron

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

***/

require_once "class/Inputs.class.php";
require_once "class/Config.class.php";
require_once "class/SubscriptionManager.class.php";
require_once "class/File.class.php";
require_once "class/FileParser.class.php";
require_once "class/FileImporter.class.php";
require_once "class/SessionRoster.class.php";

// Session initialization
SessionRoster::GetInstance();

$return_status = "null";

// Used for adding a server on the uploader
if (Inputs::GetInstance()->Post('user') == null and Inputs::GetInstance()->Post('pwd') == null and Inputs::GetInstance()->HasPostData('has_send_file') == null)
{
	$return_status = "OK";
}
// Checking if install.php is still here
else if ( file_exists("install.php") )
{
	$return_status = "901";
}
// Checking if upload is allowed
else if ( !Config::GetInstance()->IsUploaderAllowed() )
{
    $return_status = "902";
}
// Used to check authentification on roster
else if (Inputs::GetInstance()->Post('user') != null and Inputs::GetInstance()->Post('pwd') != null and !Inputs::GetInstance()->HasPostData('has_send_file'))
{
    // Checking if account is valid
    if (SubscriptionManager::GetInstance()->IsAccountValid_Raw(Inputs::GetInstance()->Post('user'), Inputs::GetInstance()->Post('pwd')))
    {
        $return_status = "OK";
    }
    else
    {
        $return_status = "001"; // Invalid account
    }
}
// Used to upload some data
else if (Inputs::GetInstance()->Post('user') != null and Inputs::GetInstance()->Post('pwd') != null and Inputs::GetInstance()->HasPostData('has_send_file')) {
    // Checking if account is valid
    if (SubscriptionManager::GetInstance()->IsAccountValid_Raw(Inputs::GetInstance()->Post('user'), Inputs::GetInstance()->Post('pwd')))
    {
        SessionRoster::GetInstance()->ConnectWithoutCookie(SubscriptionManager::GetInstance()->GetSubscription(SubscriptionManager::GetInstance()->GetAccountIdByName(Inputs::GetInstance()->Post('user'))));
		
		$file = File::GetInstance();
        
        // Checking if file has been uploaded
        if ( $file->HasFile('LZProfiler') == false )
    	{
    		$return_status = "002"; // File not found
    	}
    	else
    	{
    		$return_status = $file->CheckFileStatus('LZProfiler');
    	}
        
        if ($return_status == "OK")
    	{
    		if ($file->GetFileName('LZProfiler') != "LZProfiler.lua")
    		{
    			$return_status = "003"; // Incorrect file uploaded
    		}
    		else
    		{
    			$file_parser = new FileParser( $file->GetFileContents('LZProfiler') );
    			
    			if ( $file_parser->IsSuccessful() == false )
    			{
    				$return_status = "004"; // Invalid file format
    			}
    			else
    			{
    				$file_importer = new FileImporter($file_parser->GetParsedData());
    				
    				$return_status = $file_importer->Import(); // Returns info on the import process
    			}
    		}
    	}
    }
    else
    {
        $return_status = "001"; // Invalid account
    }
}

// Display return status
echo $return_status;
?>